<template>
    <table>
        <tr>
            <td>项目名称</td>
            <td><input type="text" v-model="info.Iname"></td>
        </tr>
        <tr>
            <td>年收益</td>
            <td><input type="text" v-model="info.Ymoney">%</td>
        </tr>
        <tr>
            <td>月收益</td>
            <td><input type="text" v-model="info.Mmoney">%</td>
        </tr>
        <tr>
            <td>最低金额</td>
            <td><input type="text" v-model="info.Minmoney">元</td>
        </tr>
        <tr>
            <td>风险等级</td>
            <td><input type="text" v-model="info.Level">级</td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" value="保存" @click="UpdateInvest">
            </td>
        </tr>
    </table>
    </template>
<script setup lang="ts">
import axios from 'axios';
import { onMounted, ref } from 'vue';
import { useRoute,useRouter } from 'vue-router';
const route = useRoute();
const Iid = route.params.Iid;
const router=useRouter()
const info=ref({
    Iname:'',
    Ymoney:0,
    Mmoney:0,
    Minmoney:0,
    Inum:0,
    Level:0,
})
onMounted(()=>{
    SelectById()
})
const SelectById=()=>{
    axios.get('https://localhost:44327/api/Lx/SelectById',{
        params:{
            Iid:Iid
        }
    }).then(res=>{
        console.log(res.data)
        info.value.Iname=res.data[0].Iname;
        info.value.Ymoney=res.data[0].Ymoney;
        info.value.Mmoney=res.data[0].Mmoney;
        info.value.Minmoney=res.data[0].Minmoney;
        info.value.Inum=res.data[0].Inum;
        info.value.Level=res.data[0].Level;
    }).catch(err=>{
        console.log(err)
    })
}


const UpdateInvest=()=>{
    if(info.value.Minmoney<100){
        alert('最低金额不能小于100')
        return
    }
    axios.post('https://localhost:44327/api/Lx/UpdateInvest',
        {
            Iname:info.value.Iname,
            Ymoney:info.value.Ymoney,
            Mmoney:info.value.Mmoney,
            Minmoney:info.value.Minmoney,
            Inum:info.value.Inum,
            Level:info.value.Level,
            Iid:Iid
        }
    )
    .then(res=>{
        if(res.data==-1){
            alert('项目名称重复，添加失败')
            return
        }
        else if(res.data>0){
            alert('修改成功')
            router.push('/ShowInvest')
            return
        }else{
            alert("修改失败")
            return
        }
    })
}

</script>
